package com.API.beans;

import com.API.dal.entities.Location;
import com.API.dal.entities.Objects;
import com.API.dal.entities.ProjectCustomer;
import com.API.dal.entities.ProjectCustomerId;
import com.API.dal.providers.CustomerProvider;
import com.API.dal.providers.LocationProvider;
import com.API.dal.providers.ProjectProvider;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.LinkedList;
import java.util.List;

@Component
@Scope("request")
public class ProjectBean {


    @Autowired
    ProjectProvider projectProvider;
    @Autowired
    CustomerProvider customerProvider;
    @Autowired
    LocationProvider locationProvider;


    ProjectCustomerId projectCustomerId = new ProjectCustomerId();
    List<Objects> projectList;
    Objects project = new Objects();
    Objects selectedProject;
    Objects customer = new Objects();
    List<Objects> customerList;
    Location location = new Location();
    List<Location> locationList;

    public List<Objects> getProjectList() {
        if (projectList == null)
            projectList = projectProvider.getProjects();
        return projectList;
    }

    public Objects getProject() {
        return project;
    }

    public void setProject(Objects project) {
        this.project = project;
    }

    public Objects getSelectedProject() {
        return selectedProject;
    }

    public void setSelectedProject(Objects selectedProject) {
        this.selectedProject = selectedProject;
    }

    public Objects getCustomer() {
        return customer;
    }

    public void setCustomer(Objects customer) {
        this.customer = customer;
    }

    public Location getLocation() {
        location = new Location();
        return location;
    }


    public List<String> getProjects(){
        List<String> projects = new LinkedList<String>();
        for (Objects pr: projectProvider.getProjects())
            projects.add(pr.getName());
        return projects;
    }

    public List<String> getLocationList() {
        List<String> locList = new LinkedList<String>();
        if (locationList == null)
            locationList = locationProvider.getLocations();
        for (Location loc: locationList)
            locList.add(loc.getName());
        return locList;
    }

    public List<String> getCustomerList() {
        List<String> cusList = new LinkedList<String>();
        if (customerList == null)
            customerList = customerProvider.getCustomers();
        for (Objects cus: customerList)
            cusList.add(cus.getName());
        return cusList;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public ProjectCustomerId getProjectCustomerId() {
        return projectCustomerId;
    }

    public void setProjectCustomerId(ProjectCustomerId projectCustomerId) {
        this.projectCustomerId = projectCustomerId;
    }

    public String getCustomer(int project_id){
        customer = projectProvider.getProjectCustomer(project_id);
        if (customer == null)
            customer = new Objects();
        return customer.getName();
    }

    public String getProjectStartDate(int project_id){
        ProjectCustomer pc = projectProvider.getProjectData(project_id);
        String str = pc.getId().getStartDate().toLocaleString();
        return str;
    }

    public String getProjectEndDate(int project_id){
        ProjectCustomer pc = projectProvider.getProjectData(project_id);
        String str;
        if (pc != null)
            str = pc.getId().getEndDate().toLocaleString();
        else
            str = new String();
        return str;
    }

    public String getProjectTotal(int project_id){
        ProjectCustomer pc = projectProvider.getProjectData(project_id);
        String str = new String();
        if (pc != null)
            str = pc.getId().getTotal().toString();
        return str;
    }

    public void addProject(){
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("addProjectDialog.show()");
    }

    public void editProject(){
        if (projectList.contains(selectedProject)){
            int id = selectedProject.getId();
            project = projectProvider.getProjectById(id);
            final RequestContext context = RequestContext.getCurrentInstance();
            context.execute("editProjectDialog.show()");
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Ошибка!",
                    "Проект не выбран!"));
        }

    }

    public void saveProject(){
        RequestContext context = RequestContext.getCurrentInstance();
        context.update("projectsForm");
        location = locationProvider.getLocationByName(location.getName());
        project.setLocation(location);
        projectProvider.addProject(project);
        customer = customerProvider.getCustomerByName(customer.getName());
        project = projectProvider.getProjectByName(project.getName());
        projectCustomerId.setProjectId(project.getId());
        projectCustomerId.setCustomerId(customer.getId());
        projectProvider.addProjectCustomer(projectCustomerId, project, customer);
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Project added successfully", project.getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("saveProject", true);

    }

    public void viewProject(){

    }


}
